<template>
  <div class="header">
    <div class="left-section">
      <el-icon class="icon"><VideoCamera /></el-icon>
      <span class="video-text">Video Call</span>
      <div class="divider"></div>
      <h1 class="title">{{ roomInfo.title }}</h1>
    </div>
    <div class="right-section">
      <!-- 观看链接 -->
      <div class="watch-link-container" @click="goWatchLink">
        {{ watchLink }}
      </div>
      <div class="watch-link" @click="copyWatchLink">
        <el-icon><Link /></el-icon>
        <span>复制观看链接</span>
      </div>
      <!-- 直播配置 -->
      <div class="live-config" @click="openLiveConfigDrawer">
        <el-icon><Setting /></el-icon>
        <span>直播配置</span>
      </div>
      <!-- 用户信息 -->
      <div class="user-info">
        <div class="user-avatar">
          <img :src="roomInfo.teacherAvatar" :alt="roomInfo.teacherName" />
        </div>
        <span class="name">{{ roomInfo.teacherName }}</span>
      </div>
    </div>
    <!-- 直播配置抽屉 -->
    <LiveConfigDrawer v-model="liveConfigDrawerVisible" />
  </div>
</template>

<script setup>
import { VideoCamera, Link } from "@element-plus/icons-vue";
import { ref } from "vue";
import { ElMessage } from "element-plus";
import LiveConfigDrawer from "@/views/live/liveConfigDrawer.vue";

const props = defineProps({
  roomInfo: Object,
});
// 添加复制链接功能
const watchLink = `${window.location.origin}/watchLive/${props.roomInfo.id}`;

// 直播配置抽屉
const liveConfigDrawerVisible = ref(false);
const openLiveConfigDrawer = () => {
  liveConfigDrawerVisible.value = true;
};

const copyWatchLink = () => {
  try {
    const textarea = document.createElement("textarea");
    textarea.value = watchLink;
    document.body.appendChild(textarea);
    textarea.select();
    document.execCommand("copy");
    document.body.removeChild(textarea);
    ElMessage.success("观看链接已复制到剪贴板");
  } catch (err) {
    // 如果 execCommand 失败，尝试 clipboard API
    fallbackClipboardCopy();
  }
};

// 方案2：使用 Clipboard API 作为后备方案
const fallbackClipboardCopy = async () => {
  try {
    await navigator.clipboard.writeText(watchLink);
    ElMessage.success("观看链接已复制到剪贴板");
  } catch (err) {
    // 如果两种方法都失败，提示手动复制
    ElMessage({
      message: "复制失败，请手动复制链接：" + watchLink,
      duration: 5000,
      showClose: true,
    });
  }
};
const goWatchLink = () => {
  window.open(watchLink, "_blank");
};
</script>

<style lang="scss" scoped>
.header {
  height: 60px;
  background: #14152b;
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);

  .left-section {
    display: flex;
    align-items: center;
    gap: 12px;

    .video-text {
      font-weight: 500;
    }

    .divider {
      width: 1px;
      height: 24px;
      background: rgba(255, 255, 255, 0.2);
    }

    .title {
      font-size: 16px;
      color: rgba(255, 255, 255, 0.9);
    }
  }

  .right-section {
    display: flex;
    align-items: center;
    gap: 20px;
    .watch-link-container {
      color: blue;
      cursor: pointer;
    }
    .live-config {
      display: flex;
      align-items: center;
      gap: 8px;
      padding: 8px 16px;
      background: rgba(255, 255, 255, 0.1);
      border-radius: 6px;
      cursor: pointer;
    }
    .watch-link {
      display: flex;
      align-items: center;
      gap: 8px;
      padding: 8px 16px;
      background: rgba(255, 255, 255, 0.1);
      border-radius: 6px;
      cursor: pointer;
      transition: all 0.3s;

      &:hover {
        background: rgba(255, 255, 255, 0.15);
      }
    }

    .user-info {
      display: flex;
      align-items: center;
      gap: 12px;
      padding: 6px;
      border-radius: 30px;
      cursor: pointer;
      transition: all 0.3s;

      &:hover {
        background: rgba(255, 255, 255, 0.15);
      }

      .user-avatar {
        width: 32px;
        height: 32px;
        border-radius: 50%;
        overflow: hidden;

        img {
          width: 100%;
          height: 100%;
          object-fit: cover;
        }
      }

      .name {
        padding: 0 8px 0 0;
        font-size: 14px;
        color: #fff;
      }
    }
  }
}
</style>
